﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.VisionModels;

namespace thinger.VisionDemo
{
    public partial class FrmImg : Form
    {
        string file;
        string filec;
        string cellid;
        ImgMsg row;
        public FrmImg(ImgMsg row)
        {
            InitializeComponent();
            Mainpro.Instance.OnGetRowRecive += OnGetRowRecive;
            this.row = row;
            this.file = this.row.file;
            this.filec = this.row.filec;
            this.cellid = this.row.cellid;
            this.Load += load_frmimg;
        }
        private void load_frmimg(object sender, EventArgs e)
        {
          
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            lab_Index.Text = cellid;
            pictureBox1.Load(filec);
            this.Text = Path.GetFileName(filec);
        }
        private void OnGetRowRecive(object sender, ImgMsg e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<object, ImgMsg>(OnGetRowRecive), sender, e);
            }
            else
            {
  
                if (e == null)
                {
                    return;
                }
             
                this.row = e;
                this.file = e.file;
                this.filec = e.filec;
                this.cellid = e.cellid;
                lab_Index.Text = cellid;
                pictureBox1.Load(filec);
                this.Text = Path.GetFileName(filec);
                btnChange.Text = "切换原图";
            }
        }
        private void btn_copy_Click(object sender, EventArgs e)
        {
            string temp;
            if (btnChange.Text == "切换cell")
            {
                temp = file;
            }
            else
            {
                temp = filec;
            }
            Clipboard.Clear();
            IDataObject data = new DataObject(DataFormats.FileDrop, new string[] { temp });
            MemoryStream memo = new MemoryStream(4);
            byte[] bytes = new byte[] { (byte)(5), 0, 0, 0 };
            memo.Write(bytes, 0, bytes.Length);
            data.SetData("ttt", memo);
            Clipboard.SetDataObject(data);
        }

        private void btn_path_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", Path.GetDirectoryName(file));

        }

        private void FrmImg_FormClosed(object sender, FormClosedEventArgs e)
        {
            Clipboard.Clear();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {

            if (btnChange.Text == "切换原图")
            {
                pictureBox1.Load(file);
                this.Text = Path.GetFileName(file);
                btnChange.Text = "切换cell";
            }
            else
            {
                pictureBox1.Load(filec);
                this.Text = Path.GetFileName(filec);
                btnChange.Text = "切换原图";
            }


        }

        private void btn_up_Click(object sender, EventArgs e)
        {
            Mainpro.Instance.ChangeRow(-1);
        }

        private void btn_next_Click(object sender, EventArgs e)
        {
            Mainpro.Instance.ChangeRow(1);
        }

     

    }
}
